package com.flag.liutao.common.handler;

import com.alibaba.fastjson.JSONObject;
import com.flag.liutao.common.domain.ApiCode;
import com.flag.liutao.common.domain.ResponseResult;
import com.flag.liutao.common.utils.WebUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 认证失败处理类
 */
@Component
@Slf4j
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
        log.error(e.getMessage());
        ResponseResult res = ResponseResult.fail(ApiCode.UNAUTHORIZED.getCode(),ApiCode.UNAUTHORIZED.getMessage());
        WebUtils.renderString(response, JSONObject.toJSONString(res));
    }
}
